var app = getApp();
// 引入SDK核心类
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
	data: {
		admin: app.globalData.domain,
		errImg: app.globalData.errimg,
		userInfo: {},
		map: {},
		types: 0, //0 未授权  1 未选中经销商 
		storeList: [] //经销商
	},
	onLoad(options) {
		var that = this;
		// 实例化API核心类
		qqmapsdk = new QQMapWX({
			key: 'UQ6BZ-UH3WI-5UZG7-5EZVY-LRGUV-REBTI'
		});
	},
	onShow: function() {
		let vm = this;
		vm.getUserLocation();
	},
	getUserLocation: function() {
		let vm = this;
		wx.getSetting({
			success: (res) => {
				if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
					wx.showModal({
						title: '请求授权当前位置',
						content: '需要获取您的地理位置，请确认授权',
						success: function(res) {
							if (res.cancel) {
								wx.showToast({
									title: '拒绝授权',
									icon: 'none',
									duration: 1000
								})
							} else if (res.confirm) {
								wx.openSetting({
									success: function(dataAu) {
										if (dataAu.authSetting["scope.userLocation"] == true) {
											wx.showToast({
												title: '授权成功',
												icon: 'success',
												duration: 1000
											})
											//再次授权，调用wx.getLocation的API
											vm.getLocation();
										} else {
											wx.showToast({
												title: '授权失败',
												icon: 'none',
												duration: 1000
											})
										}
									}
								})
							}
						}
					})
				} else if (res.authSetting['scope.userLocation'] == undefined) {
					//调用wx.getLocation的API
					vm.getLocation();
				} else {
					//调用wx.getLocation的API
					vm.getLocation();
				}
			}
		})
	},
	// 微信获得经纬度
	getLocation: function() {
		let vm = this;
		wx.getLocation({
			type: 'wgs84',
			success: function(res) {
				var latitude = res.latitude
				var longitude = res.longitude
				var speed = res.speed
				var accuracy = res.accuracy;
				vm.getLocal(latitude, longitude)
			},
			fail: function(res) {
				console.log('fail' + JSON.stringify(res))
			}
		})
	},
	// 获取当前地理位置
	getLocal: function(latitude, longitude) {
		let vm = this;
		qqmapsdk.reverseGeocoder({
			location: {
				latitude: latitude,
				longitude: longitude
			},
			success: function(res) {
				let province = res.result.ad_info.province
				let city = res.result.ad_info.city
				let district = res.result.address_component.district
				vm.setData({
					map: {
						province: province,
						city: city,
						latitude: latitude,
						longitude: longitude,
						district: district
					}
				})
				wx.setStorage({
					key: "map",
					data: vm.data.map //需要存储的数据
				})
				//GET   获取经销商列表
				app.globalAjax(
					'/api/store/list',
					"get", {
						longitude: longitude,
						latitude: latitude
					},
					function(e) {
						if (e.code == 0) {
							vm.setData({
								storeList: e.storeList
							});
						}
					}
				);

			},
			fail: function(res) {
				console.log('login.js 219', res);
			},
			complete: function(res) {
				// console.log(res);
			}
		});
	},
	errImg: function(e) {
		var _errImg = e.target.dataset.errImg;
		var _objImg = "'" + _errImg + "'";
		var _errObj = {};
		_errObj[_errImg] = this.data.errImg;
		this.setData(_errObj); //注意这里的赋值方式...
	},
	selectdor: function(event) { //选择经销商
		var that = this;
		var i = that.data.storeList[event.currentTarget.dataset.index]
		//设置缓存
		wx.setStorage({
			key: "dealer",
			data: i
		});
		wx.reLaunch({
			url: '/pages/index/index'
		})
		
	},
	applyGroup: function(event){ //若附近三公里没有团，跳转申请当团长
		wx.navigateTo({
			url: '/pages/member/group/group'
		})
	}
})
